package com.winit.requirement.service;

import com.winit.common.CloudLinkSend.MqMsgType;
import com.winit.common.CloudLinkSend.MsgSend;
import com.winit.common.base.bean.ExecuteResult;
import com.winit.common.base.bean.Status;
import com.winit.requirement.manager.BrdRequirementManager;
import com.winit.requirement.model.BrdRequirement;
import com.winit.requirement.model.ReqHistory;
import com.winit.requirement.repository.BrdRequirementRepository;
import com.winit.requirement.repository.BrdRequirementRepositoryOperation;
import com.winit.requirement.repository.ReqHistoryRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class BrdRequirementService implements IBrdRequirementService{

    @Autowired
    private BrdRequirementRepository brdRequirementRepository;
    @Autowired
    private ReqHistoryRepository reqHistoryRepository;
    @Autowired
    private BrdRequirementManager brdRequirementManager;
    @Autowired
    private BrdRequirementRepositoryOperation brdRequirementRepositoryOperation;

    @Autowired
    MsgSend msgSend;

    @Override
    public int deleteBrdRequirementsByBrdId(String brdId) {
        return this.brdRequirementRepository.updateIsDeletedByBrdId(brdId, "0");
    }

    @Override
    public ExecuteResult checkCanBrdRequirementDeleteForBrd(String brdId) {
        ExecuteResult rs = new ExecuteResult();
        List<BrdRequirement> requirementList = this.brdRequirementRepository.searchBrdRequirements(brdId, null, null, "0");
        if (!CollectionUtils.isEmpty(requirementList)) {
            for (BrdRequirement requirement : requirementList) {
                if("0".equals(requirement.getReqIsDel())){  //本身已经删除了就不再校验是否可被删除了
                    continue;
                }
                if(!this.brdRequirementManager.canBrdRequirementEntityDelete(requirement, rs)){
                    return rs;
                }
            }
        }
        return rs;
    }

    @Override
    public List<BrdRequirement> searchBrdRequirementsByReqIds(List<String> reqIds) {
        return this.brdRequirementRepository.searchBrdRequirementsByReqIds(reqIds);
    }

    @Override
    public void flushBrdRequirementsOfProjectPlan(String projectPlanId, List<String> reqIds, int operationMode) {
        Map map= new HashMap<>();
        if (2 == operationMode){        //删除或者修改时都要删除原有的依赖关系
//            this.brdRequirementRepository.updateProjectPlanIdAndBrdStatusByProjectPlanId(projectPlanId, null, BrdStatus.DISSOCIATION.name());
            this.brdRequirementRepository.updateProjectPlanIdByProjectPlanId(projectPlanId, null);
            this.brdRequirementRepository.updateNotOnlineBrdStatusByProjectPlanId(projectPlanId, Status.DISSOCIATION.name());
        }
        if( 3 == operationMode){
            this.brdRequirementRepository.updateProjectPlanIdByProjectPlanId(projectPlanId, null);
            this.brdRequirementRepository.updateNotOnlineBrdStatusByProjectPlanId(projectPlanId, Status.DISSOCIATION.name());
//            map.put("reqIds",reqIds);
//            map.put("planId",projectPlanId);
//            msgSend.sendReqMsg(MqMsgType.DISSOCIATION,map);
//            this.brdRequirementRepository.updateProjectPlanIdAndBrdStatusByProjectPlanId(projectPlanId, null, BrdStatus.DISSOCIATION.name());
        }
        if (3 == operationMode || 1 == operationMode) {       //修改或者新建prd的时候需要（重新）建立关系
            if (CollectionUtils.isEmpty(reqIds)){   //检测为空时返回
                return;
            }
//            this.brdRequirementRepository.updateProjectPlanIdAndBrdStatusByReqIds(reqIds, projectPlanId, BrdStatus.P_PLAN.name());
            this.brdRequirementRepository.updateProjectPlanIdByReqIds(reqIds, projectPlanId);
            this.brdRequirementRepository.updateBrdStatusByReqIdsIfNotOnline(reqIds, Status.P_PLAN.name());
            map.put("reqIds",reqIds);
            map.put("planId",projectPlanId);
            msgSend.sendReqMsg(MqMsgType.PROJECT,map);
        }
    }



    @Override
    public void updateBrdById(BrdRequirement req) {
        this.brdRequirementRepositoryOperation.updateBrdRequirementById(req);
    }

    @Override
    public void updateProjectPlanIdAndBrdStatusByProjectPlanId(String reqId, String status, String projectPlanId) {
//        this.brdRequirementRepository.updateProjectPlanIdAndBrdStatusByReqId(reqId, projectPlanId, status);
        List<String> reqIds = new ArrayList<>();
        reqIds.add(reqId);
        this.brdRequirementRepository.updateProjectPlanIdByReqIds(reqIds, projectPlanId);
        this.brdRequirementRepository.updateBrdStatusByReqIdsIfNotOnline(reqIds, status);
        Map map=new HashMap<>();
        map.put("reqIds",reqIds);
        map.put("planId",projectPlanId);
        if(Status.DISSOCIATION.name().equals(status)){
            msgSend.sendReqMsg(MqMsgType.DISSOCIATION,map);
        }else{
            msgSend.sendReqMsg(MqMsgType.PROJECT,map);
        }
    }

    @Override
    public BrdRequirement getBrdRequirementByReqId(String reqId) {
        return this.brdRequirementRepository.findOne(reqId);
    }

    @Override
    public ReqHistory getLatestSubmitHistory(String reqId) {
        return this.reqHistoryRepository.findFirstByReqIdOrderByCreateTimeDesc(reqId);
    }
}
